package com.apexedu.biz.service.impl;

import com.apexedu.biz.Const;
import com.apexedu.biz.action.SystemListAction;
import com.apexedu.biz.entity.TAccessAppReg;
import com.apexedu.framework.service.impl.AbstractBaseServiceSupport;
import com.apexedu.framework.util.ConfigUtil;
import com.apexedu.framework.util.DBUtil;
import com.apexedu.framework.util.Pager;
import com.apexedu.framework.util.SeqFactory;
import com.apexedu.identity.action.IdLoginAction;
import com.apexedu.portal.entity.TDb;
import com.apexedu.sys.util.BusinessLogUtil;
import com.apexedu.util.StringUtil;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("systemListService")
public class SystemListServiceImpl extends AbstractBaseServiceSupport {

    @SuppressWarnings("unchecked")
    @Override
    public List<?> list(Object myaction, Pager pager) throws Exception {
        SystemListAction action = (SystemListAction) myaction;
        String sql = " from T_ACCESS_APP_REG t where 1=1 ";
        String sqlColumn = "select appid,appname,appcode,appurl,xtlx,"
                + "(select zdmc from T_SYS_CODE where zdlb='BOOLEAN' and zdbm=t.state) as stateText," +
                "memo,sync_Direction as syncDirection,tblx,driver,url,name,password";
        String type = action.getParameter("type");
        if (StringUtil.isNotBlank(type)) {
            sql += " and t.state = 1 and t.appid not in ('" + ConfigUtil.getString("AuCenterAccessEnterCode") + "','" + ConfigUtil.getString("portalCode") + "')";
            action.setParameter("type", type);
        }
        pager.setTotalRows(DBUtil.count(sql));
        List<TAccessAppReg> list = DBUtil.queryPageBeanList(pager, sqlColumn + sql + " order by ordernum desc,appid",
                TAccessAppReg.class);
        return list;
    }

    @Override
    public void load(Object myaction) throws Exception {
        SystemListAction action = (SystemListAction) myaction;
        String wid = action.getWid();
        TAccessAppReg accessAppReg = getBaseDao().findById(TAccessAppReg.class, wid);
        action.setAccessAppReg(accessAppReg);
    }

    @Override
    public boolean remove(Object myaction) throws Exception {
        SystemListAction action = (SystemListAction) myaction;
        String ids = action.getWid();
        boolean deleteSuccess = getBaseDao().deleteAll("TAccessAppReg", "appid", "=", ids);
        BusinessLogUtil.log(action.getUserloginid(), Const.CZDX_T_ACCESS_APP_REG, Const.LOG_ACTION_DEL,
                IdLoginAction.getIpAddr(action.getRequest()));
        return deleteSuccess;
    }

    @Override
    public void saveOrUpdate(Object myaction) throws Exception {
        SystemListAction action = (SystemListAction) myaction;
        TAccessAppReg accessAppReg = action.getAccessAppReg();

        if (StringUtil.isNotBlank(accessAppReg.getDb())) { //获取数据源信息
            TDb db = DBUtil.queryBean("select * from T_db where code = ?", TDb.class, accessAppReg.getDb());
            accessAppReg.setDriver(db.getDriver());
            accessAppReg.setUrl(db.getUrl());
            accessAppReg.setName(db.getUsername());
            accessAppReg.setPassword(db.getPwd());
        }
        if (accessAppReg != null) {        //只是编辑价格
            if (StringUtil.isNotBlank(accessAppReg.getAppid())) {
                baseDao.updateNotNull(accessAppReg);
                BusinessLogUtil.log(action.getUserloginid(), Const.CZDX_T_ACCESS_APP_REG, Const.LOG_ACTION_UPDATE,
                        IdLoginAction.getIpAddr(action.getRequest()));
            } else {
                accessAppReg.setAppid(SeqFactory.getNewSequenceAlone());
                baseDao.save(accessAppReg);
                BusinessLogUtil.log(action.getUserloginid(), Const.CZDX_T_ACCESS_APP_REG, Const.LOG_ACTION_SAVE,
                        IdLoginAction.getIpAddr(action.getRequest()));
            }
        }
    }

}
